﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUCountry
    {
        /// <summary>
        /// Get All the Country of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Country> GetAllCountry(bool searchInDeleted)
        {
            List<Country> Countrys = new List<Country>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Country in db.Country
                              where Country.cou_isdeleted == searchInDeleted
                              select Country;
                foreach (Country tmp in results)
                    Countrys.Add(tmp);
            }

            return Countrys;
        }

        /// <summary>
        /// Get the Country by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Country GetCountryByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var cou = from Country in db.Country
                              where Country.cou_id == id
                              select Country;
                    foreach (Country tmp in cou)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// cou a Country into the database.
        /// </summary>
        /// <param name="cou"></param>
        /// <returns></returns>
        public static Country Countryrea(Country cou)
        {
            if (cou != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Country ncou = db.Country.Add(cou);
                    db.SaveChanges();
                    return ncou;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Country of the database.
        /// </summary>
        /// <param name="cou"></param>
        /// <returns></returns>
        public static bool RemoveCountry(Country cou)
        {
            if (cou != null)
            {
                cou.cou_isdeleted = true;
                if (UpdateCountry(cou) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Country into the database.
        /// </summary>
        /// <param name="cou"></param>
        /// <returns></returns>
        public static Country UpdateCountry(Country cou)
        {
            if (cou != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Country in db.Country
                                  where Country.cou_id == cou.cou_id
                                  select Country;

                    foreach (var tmp in current)
                    {
                        tmp.cou_label = cou.cou_label;
                        tmp.are_id = cou.are_id;
                        tmp.cou_isdeleted = cou.cou_isdeleted;

                        db.SaveChanges();
                    }

                    cou = current as Country;
                }
            }

            return cou;
        }
    }
}
